<?php
class ChannelCategory extends KF_Auth_Controller{
	/*
        药店列表
    */
    public function index()
    {   
        if(IS_POST)
        {
            $where = array();
            $config = array(
                'table' => 'okdkyao.dk_app_yaodian',
                'columns' => array(
                    array('db' => 'ID'),
                    array('db' => 'Caption')
                ),
                'where' => implode(' and ', $where)
            );
            $this->load->library('dataTables', $config);
            $result = $this->datatables->search();
            
            exit(json_encode($result));
        }
        
        $this->load->view("channel_category/index",$res);
    }

    /*
        渠道列表
    */
    public function channelList()
    {
        if(IS_POST){
            $this->load->model("Common_model", "CommonModel");
            $CommonModel = $this->CommonModel;

            $channel = $CommonModel->getAllChannelInfo();

            $res = array(
                "status" => "success",
                "data" => array_values($channel)
            );

            header('Content-type: application/json');
            echo json_encode($res);
        }else{
            $this->load->view("channel_category/channel_list", array());
        }
    }

    /*
        分类列表
    */
    public function categoryList()
    {
        $chn_id = $_GET['chn_id'];
        $store_id = $_GET['store_id'];

        if(IS_POST){
            $this->load->model("ChannelCategory_model", "ChannelCategoryModel");
            $ChannelCategoryModel = $this->ChannelCategoryModel;

            $categoryList = $ChannelCategoryModel->getCategoryList($chn_id, $store_id);

            $res = array(
                "status" => "success",
                "data" => $categoryList
            );
            header('Content-type: application/json');
            echo json_encode($res);
        }else{
            $this->load->model("Common_model", "CommonModel");
            $CommonModel = $this->CommonModel;
            $channel = $CommonModel->getAllChannelInfo($chn_id);

            $data = array(
                "chn_name" => $channel['name']
            );
            $this->load->view("channel_category/category_list", $data);
        }
    }

    /*
        添加分类
    */
    public function addCategory()
    {
        if(IS_POST){
            $chn_id = $_POST['chn_id'];
            $store_id = $_POST['store_id'];
            $category_id = $_POST['category_id'];
            $category_name = $_POST['category_name'];
            $category_rank = $_POST['category_rank'];

            $this->load->model("ChannelCategory_model", "ChannelCategoryModel");
            $ChannelCategoryModel = $this->ChannelCategoryModel;

            $add = $ChannelCategoryModel->addCategory($chn_id, $store_id, $category_id, $category_name, $category_rank);

            header('Content-type: application/json');
            echo json_encode($add);
        }
    }

    /*
        删除分类
    */
    public function delCategory()
    {
        if(IS_POST){
            $chn_id = $_POST['chn_id'];
            $store_id = $_POST['store_id'];
            $other_category_id = $_POST['other_category_id'];

            $this->load->model("ChannelCategory_model", "ChannelCategoryModel");
            $ChannelCategoryModel = $this->ChannelCategoryModel;

            $del = $ChannelCategoryModel->delCategory($chn_id, $store_id, $other_category_id);

            header('Content-type: application/json');
            echo json_encode($del);
        }
    }

    /*
        更新分类信息
    */
    public function updateCategory()
    {
        
        if(IS_POST){
            $chn_id = $_POST['chn_id'];
            $store_id = $_POST['store_id'];
            $other_category_id = $_POST['other_category_id'];
            $other_category_name = $_POST['category_name'];
            $other_category_rank = $_POST['category_rank'];

            $this->load->model("ChannelCategory_model", "ChannelCategoryModel");
            $ChannelCategoryModel = $this->ChannelCategoryModel;

            $update = $ChannelCategoryModel->updateCategory($chn_id, $store_id, $other_category_id, $other_category_name, $other_category_rank);

            header('Content-type: application/json');
            echo json_encode($update);
        }
    }

    /*
        初始化分类列表
    */
    public function initCategory()
    {
        if(IS_POST){
            $chn_id = $_POST['chn_id'];
            $store_id = $_POST['store_id'];

            $this->load->model("ChannelCategory_model", "ChannelCategoryModel");
            $ChannelCategoryModel = $this->ChannelCategoryModel;

            $init = $ChannelCategoryModel->initCategory($chn_id, $store_id);

            header('Content-type: application/json');
            echo json_encode($update);
        }
    }
    
}